package com.ttc.ssm.controller.home;

import com.ttc.ssm.controller.common;
import com.ttc.ssm.po.Company;
import com.ttc.ssm.po.Customer;
import com.ttc.ssm.po.Zone;
import com.ttc.ssm.service.home.HomeCommonService;
import com.ttc.ssm.service.home.HomeCompanyService;
import com.ttc.ssm.service.home.HomeCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Jiarui on 2016/8/4.
 */
@Controller
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    HomeCustomerService homeCustomerService;

    @Autowired
    HomeCommonService homeCommonService;

    @Autowired
    HomeCompanyService homeCompanyService;

    @RequestMapping("/customer")
    public String customer(){
        return "home/register/customer";
    }

    @RequestMapping("/company")
    public String company(Model model) throws Exception {
        List<Zone> zones = homeCommonService.getZone();
        model.addAttribute("zone_list",zones);
        return "home/register/company";
    }

    @RequestMapping("/cus_submit")
    public String cus_submit(Customer customer, String confirm_password, Model model, HttpServletRequest request, HttpSession session) throws Exception {
        if("".equals(customer.getFullname())||customer.getFullname() == null || !common.isstring(customer.getFullname())){
            model.addAttribute("customer",customer);
            model.addAttribute("confirm_password",confirm_password);
            model.addAttribute("msg",1);
            return "forward:/register/customer.action";
        }
        if("".equals(customer.getEmail())||customer.getEmail() == null){
            model.addAttribute("customer",customer);
            model.addAttribute("confirm_password",confirm_password);
            model.addAttribute("msg",2);
            return "forward:/register/customer.action";
        }
        if(!common.checkEmail(customer.getEmail())){
            model.addAttribute("customer",customer);
            model.addAttribute("confirm_password",confirm_password);
            model.addAttribute("msg",6);
            return "forward:/register/customer.action";
        }
        if("".equals(customer.getTelephone())||customer.getTelephone() == null){
            model.addAttribute("customer",customer);
            model.addAttribute("confirm_password",confirm_password);
            model.addAttribute("msg",3);
            return "forward:/register/customer.action";
        }
        if(!common.checkMobileNumber(customer.getTelephone())){
            model.addAttribute("customer",customer);
            model.addAttribute("confirm_password",confirm_password);
            model.addAttribute("msg",7);
            return "forward:/register/customer.action";
        }
        if("".equals(customer.getPassword())||customer.getPassword() == null || customer.getPassword().length() < 6 || customer.getPassword().length()>20){
            model.addAttribute("customer",customer);
            model.addAttribute("confirm_password",confirm_password);
            model.addAttribute("msg",4);
            return "forward:/register/customer.action";
        }
        if("".equals(confirm_password)||confirm_password == null){
            model.addAttribute("customer",customer);
            model.addAttribute("confirm_password",confirm_password);
            model.addAttribute("msg",5);
            return "forward:/register/customer.action";
        }
        if(!customer.getPassword().equals(confirm_password)){
            model.addAttribute("customer",customer);
            model.addAttribute("confirm_password",confirm_password);
            model.addAttribute("msg",8);
            return "forward:/register/customer.action";
        }
        Map map = new HashMap();
        map.put("email",customer.getEmail());
        map.put("telephone",null);
        if(homeCustomerService.checkExist(map) >= 1){
            model.addAttribute("customer",customer);
            model.addAttribute("confirm_password",confirm_password);
            model.addAttribute("msg",9);
            return "forward:/register/customer.action";
        }
        Map map1 = new HashMap();
        map1.put("email",null);
        map1.put("telephone",customer.getTelephone());
        if(homeCustomerService.checkExist(map1) >= 1){
            model.addAttribute("customer",customer);
            model.addAttribute("confirm_password",confirm_password);
            model.addAttribute("msg",10);
            return "forward:/register/customer.action";
        }
        try{
            int customer_id = homeCustomerService.register_customer(customer,request);
            session.setAttribute("fullname", customer.getFullname());
            session.setAttribute("customer_id",customer_id);
            session.setAttribute("type","0");
            model.addAttribute("success",1);
            return "forward:/register/customer.action";
        }catch (Exception e){
            model.addAttribute("customer",customer);
            model.addAttribute("confirm_password",confirm_password);
            model.addAttribute("error",1);
            return "forward:/register/customer.action";
        }
    }

    @RequestMapping("/com_submit")
    public String com_submit(Company company,String confirm_password,Model model,HttpServletRequest request, HttpSession session) throws Exception {
        if("".equals(company.getCompany_name()) || "".equals(company.getContact()) || "".equals(company.getContact_sex()) ||
                "".equals(company.getZone_num()) || "".equals(company.getPhone()) || "".equals(company.getTelephone()) ||
                "".equals(company.getFullname()) || "".equals(company.getE_mail()) || "".equals(company.getPassword()) || company.getCity_id() == 0
                || "".equals(confirm_password)){
            model.addAttribute("company",company);
            model.addAttribute("confirm_password",confirm_password);
            model.addAttribute("error",1);
            return "forward:/register/company.action";
        }
        if(homeCustomerService.getCompanyByEmail(company.getE_mail())!=null){
            return "forward:/register/company.action";
        }
        try{
            int company_id = homeCompanyService.register_company(company,request);
            session.setAttribute("fullname", company.getFullname());
            session.setAttribute("company_id",company_id);
            session.setAttribute("type","1");
            model.addAttribute("success",1);
            return "forward:/register/company.action";
        }catch (Exception e){
            model.addAttribute("company",company);
            model.addAttribute("confirm_password",confirm_password);
            model.addAttribute("error",1);
            return "forward:/register/company.action";
        }
    }

}
